package org.kms.Controllers;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.kms.dao.recruitDao;
import org.kms.dao.staffDao;
import org.kms.pojo.*;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Recruit")
@CrossOrigin(value = "*")
public class recruit {

    @Autowired
    private recruitDao recruitDao;
    @Autowired
    private staffDao staffDao;

   @RequestMapping("/all")
    @ResponseBody
    private ResponseResult<List<Recruit>> all(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Recruit condition)throws  Exception{
       Map<String,Object> map = new HashMap<>();

       Page<Recruit> pager = PageHelper.startPage(page,limit);
       List<Recruit> list = recruitDao.getConditionList(condition);

       return new ResponseResult(list, pager.getTotal());
    }

    @RequestMapping("/cNameAndcid")
    @ResponseBody
    public List<Recruit> Rows(Recruit recruit)throws Exception{
       return recruitDao.getbyscDate(recruit);
    }

    @RequestMapping("/cNamegetCid")
    @ResponseBody
    public int getcid(String cName)throws Exception{
        return recruitDao.getbycName(cName);
    }


    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Staff> all()throws Exception{
        return staffDao.getAll();
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Recruit recruit)throws Exception{
        if (recruit.getiRefund() == null){
            recruit.setiRefund("否");
        }else{
            recruit.setiRefund("是");
        }
        if (recruit.getDpay() == null){
            recruit.setDpay("否");
        }else{
            recruit.setDpay("是");
        }
        recruitDao.add(recruit);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/update")
    @ResponseBody
    private ResponseResult update(Recruit recruit)throws Exception{
        if (recruit.getiRefund() == null){
            recruit.setiRefund("否");
        }else{
            recruit.setiRefund("是");
        }
        if (recruit.getDpay() == null){
            recruit.setDpay("否");
        }else{
            recruit.setDpay("是");
        }
        recruitDao.update(recruit);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/updbycidtostate")
    @ResponseBody
    public ResponseResult updstate(Integer cid) throws Exception{

       recruitDao.getbycidUpState(cid);
       return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/updbycidtostate2")
    @ResponseBody
    public ResponseResult updstate2(Integer cid) throws Exception{

        recruitDao.getbycidUpState2(cid);
        return ResponseResult.createSuccessResult();
    }

}
